// pages/Goods/Address/index.js
var common = require("../../../../public.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    token: '',
    userInfo: {},
    addressList: [],
    from: '', //页面进入
    active: '',
    isLoadingBusy: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (opt) {
    const that = this
    common.getLocalUserInfo(function (success, data) {
      if (success) {
        that.setData({
          userInfo: data.user,
          token: data.token,
          itemId: opt.itemId ? opt.itemId : '',
          storeId: opt.storeId ? opt.storeId : '',
          from: opt.from ? opt.from : '',
          active: opt.active ? opt.active : '',
        }, function () {
          that.getList()
        })
      }
    })

    // if(options.from=='mySelf'){ // 从个人中心进入
    // }else{ // 从商品详情进入

    // }
  },
  onShow() {
    // this.getList()
  },
  addAddress() { // 添加新地址
    wx.navigateTo({
      url: '/pages/Goods/Address/addAddress/index?from=0',
    })
  },
  changeAddress(e) { // 修改地址
    // console.log('address',e.currentTarget.dataset.item)
    let item = JSON.stringify(e.currentTarget.dataset.item)
    wx.navigateTo({
      url: '/pages/Goods/Address/addAddress/index?from=1&item=' + item,
    })
  },
  getList() {
    this.setData({
      isLoadingBusy: true
    })
    const that = this
    const param = {
      token: that.data.token,
      userId: that.data.userInfo.id
    }
    if (that.data.addressList.length > 0) {
      that.setData({
        addressList: []
      })
    }

    common.getJson('/user/ship/list', param, function (res) {
      if (res.status === 200) {
        that.setData({
          addressList: res.data
        })
      }
    },
      function () {
        setTimeout(function () {
          that.setData({
            isLoadingBusy: false
          })
        }, 200);
      })
  },
  clickgetId(e) {
    let that = this
    if (this.data.from == "mySelf") return;
    // that.setData({
    //   active: that.options.active,
    //   receiverName: that.options.receiverName,
    //   receiverMobile: that.options.receiverMobile,
    //   receiverAddress: that.options.receiverAddress,
    //   tab3: false,
    //   tab2: true,
    //   package: 0
    // })

    var pages = getCurrentPages()
    var prevPage = pages[pages.length - 2] //上一个页面
    const options = {
      // active: 2,
      storeId: that.data.storeId,
      itemId: that.data.itemId,
      selectedAddr: true, // false代表已经选择地址
      receiverName: e.currentTarget.dataset.name,
      receiverMobile: e.currentTarget.dataset.phone,
      receiverAddress: e.currentTarget.dataset.address,
      id: e.currentTarget.dataset.id,
    }

    wx.navigateBack({
      delta: 1,
      success: function () {
        prevPage.onLoad(options)
      }
    })
    // wx.navigateTo({
    //   url: "../Commit/index?active=2&storeId=" + this.data.storeId + '&itemId=' + this.data.itemId + '&receiverName=' + e.currentTarget.dataset.name + '&receiverMobile=' + e.currentTarget.dataset.phone + '&receiverAddress=' + e.currentTarget.dataset.address + '&index1=' + this.data.index1 + '&id=' + e.currentTarget.dataset.id
    // })
  }
})